package org.ls.wxscancodes.controller;

import cn.hutool.core.util.XmlUtil;
import com.qq.weixin.mp.aes.AesException;
import com.qq.weixin.mp.aes.SHA1;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import org.ls.wxscancodes.util.PageController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.w3c.dom.Document;
import org.w3c.dom.Node;

import java.io.*;

@RestController
public class TestConntroller {
    //这个就认证时候使用一次
//    /**
//     * 后台配置秘钥
//     */
//    private  String token="qwertyu";
//
//    /**
//     * 测试账号配置什么都可以
//     * @param signature
//     * @param timestamp
//     * @param nonce
//     * @param echostr
//     * @return
//     * @throws AesException
//     */
//    @RequestMapping("/wechat")
//    public  String check(String signature,String timestamp,String nonce,String echostr) throws AesException {
//        String sign = SHA1.getSHA1(token, timestamp, nonce, "");
//        //如果签名一致就返回这个
//        if(sign.equals(signature)){
//            return echostr;
//        }
//        return "随机返回";
//    }
//
//    /**
//     * 正式账号要带.html
//     * @param signature
//     * @param timestamp
//     * @param nonce
//     * @param echostr
//     * @return
//     * @throws AesException
//     */
//    @RequestMapping("/index.html")
//    public  String index(String signature,String timestamp,String nonce,String echostr) throws AesException {
//        String sign = SHA1.getSHA1(token, timestamp, nonce, "");
//        //如果签名一致就返回这个
//        if(sign.equals(signature)){
//            return echostr;
//        }
//        return "随机返回";
//    }


    @RequestMapping("/wechat")
    public  void check(HttpServletResponse resp, HttpServletRequest req) throws AesException {
        try(BufferedReader br =new  BufferedReader(new InputStreamReader(req.getInputStream()));) {
        String line =null;
        StringBuilder sb=new  StringBuilder();
        while((line= br.readLine())!=null){
            sb.append(line);
        }
            String xml = sb.toString();
            System.out.println(xml);
            Document document = XmlUtil.parseXml(xml);
            Node typeNode = document.getElementsByTagName("MsgType").item(0);
            System.out.println(typeNode.getTextContent());
            String type=typeNode.getTextContent();
            switch (type){
                case "event"://事件
                    handleEvent(document,resp);
                    break;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }
    public  void handleEvent(Document document,HttpServletResponse resp) throws IOException {
        String event=  document.getElementsByTagName("Event").item(0).getTextContent();
        String openId=  document.getElementsByTagName("FromUserName").item(0).getTextContent();
        switch (event){
            case "subscribe":
                System.out.println("关注了");
                break;
            case "unsubscribe":
                System.out.println("取关了");
                break;
            case "SCAN":
                System.out.println("扫码了");
                String ticket=document.getElementsByTagName("Ticket").item(0).getTextContent();
                String evenKey=document.getElementsByTagName("EventKey").item(0).getTextContent();
                if("login".equals(evenKey)){
                    PageController.QR_CODES.put(ticket,"登录了");
                    //处理登录事件
                    PrintWriter writer =resp.getWriter();

                    writer.write("");
                    writer.flush();
                    writer.close();
                }
                break;
        }
    }

}
